
package com.seekting.gongzhu.sdk;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public abstract class NetWorkHelper {

    protected String targetIp;
    protected int waitTime = 1000;
    protected int mIndex;
    protected Socket socket;
    protected DataOutputStream out;
    protected DataInputStream in;
    protected Receiveable receiveable;
    private DisConnectListener disconnectListener;

    public NetWorkHelper(Socket socket, DataOutputStream out, DataInputStream in, int index,
            DisConnectListener disconnectListener, Receiveable receiveable) {
        this.in = in;
        this.out = out;
        this.socket = socket;
        this.mIndex = index;
        InetAddress inetAddress = socket.getInetAddress();
        targetIp = inetAddress.getHostAddress() + ":" + socket.getPort();
        this.disconnectListener = disconnectListener;

    }

    public void startRecMsg(final Receiveable receiveable) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                KLog.d("开始接收消息");
                while (true) {

                    if (in == null) {
                        continue;
                    }
                    try {
                        String str = in.readUTF();
                        if (!TextUtils.isEmpty(str)) {
                            if (receiveable != null) {
                                receiveable.onReceive(targetIp, str);
                            }
                        }
                    } catch (IOException e) {
                        if (disconnectListener != null) {
                            disconnectListener.onDisConnect(mIndex, targetIp);
                        }
                        return;
                    }
                }
            }
        }).start();

    }

    public void send(String msg) {
        try {
            out.writeUTF(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void testgetMessage() {
        while (true) {

            if (in != null) {
                try {
                    String s = in.readUTF();
                    System.out.println("testgetMessage" + s);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void testSendMessage() {

        while (true) {

            if (out != null) {
                try {
                    out.writeUTF("xxxxx");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public DataOutputStream getOut() {
        return out;
    }

    public void setOut(DataOutputStream out) {
        this.out = out;
    }

    public DataInputStream getIn() {
        return in;
    }

    public void setIn(DataInputStream in) {
        this.in = in;
    }

    public String getTargetIp() {
        return targetIp;
    }

}
